using GLib;

public class DirExplorer : GLib.Object 
{
    private File[] dirs;
    
    public DirExplorer(string[] args)
    {
        foreach(string arg in args)
        {
            if(null != arg)
            {
                dirs += File.new_for_commandline_arg(arg);
            }
        }
    }
    
    public bool check()
    {
        foreach(File dir in dirs)
        {
            if(!dir.query_exists())
            {
                print("The dircetory %s is not exit, please insure the directoris you input is exists!\n", dir.get_basename());
                return false;
            }
        }
        
        return true;
    }
    
    public Iterator iterator()
    {
        return new Iterator(this);
    }
    
    public class Iterator
    {
        private int index = -1;
        private string cur_name = null;
        private Dir cur_dir = null;
        private DirExplorer dir_exploer;
        
        public Iterator(DirExplorer dir_exploer)
        {
            this.dir_exploer = dir_exploer;
        }

        public bool next()
        {
            if(null != this.cur_dir)
            {
                do
                {
                    this.cur_name = this.cur_dir.read_name();
                    
                } while(null != this.cur_name && !this.cur_name.has_suffix(".c"));
            }
            
            if(null == this.cur_name)
            {   
                do
                {
                    this.index++;
                    
                    if(null == this.dir_exploer.dirs[this.index])
                    {
                        return false;
                    }
                    
                    try
                    {
                        this.cur_dir = Dir.open(this.dir_exploer.dirs[this.index].get_path());
                    }
                    catch(FileError error)
                    {
                        print("%s\n", error.message);
                    }
                    
                    if(null  == this.cur_dir)
                    {
                        return false;
                    }
                    
                    do
                    {
                        this.cur_name = this.cur_dir.read_name();
                        
                        if(null != this.cur_name 
                        && this.cur_name.has_suffix(".c")
                        && this.cur_name != "auto-test-temp.c")
                        {
                            break;
                        }
                        
                    } while(null != this.cur_name);   
                }
                while(null == this.cur_name);
            }
            
            return true;
        }
        
        public string @get()
        {
            return Path.build_path (Path.DIR_SEPARATOR_S, this.dir_exploer.dirs[this.index].get_path(),  this.cur_name);
        }
    }
}
